<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );
class Exchange {
    private $CI;
		
	public function __construct() {
        $this->CI = & get_instance ();
        
        $this->CI->load->model('product_model');
        $this->CI->load->model('currency_model');
        $this->CI->load->model('exchange_rate_model');
        
	}
	
	/**
	 * 
	 * Trasnforms the given value to the given currency
	 * @param integer $value
	 * @param string $currency
	 * @param bool $productPrice
	 */	
	public function transformPrice($value, $currency = '', $productPrice = false, $prod_id = 0,  $discount_rate = false, $countryId = false, $promotion = 0, $no_vat = false){
		if (empty($currency)){
			$currency = $this->CI->config->item('def_currency');
		}

		if ($productPrice) {
			/*$gbpCurrencyObj = $this->CI->currency_model->get_currency('GBP');
			$gbpExchangeRate = $this->CI->exchange_rate_model->get_rate_by_currency($gbpCurrencyObj->id);

			$value = ceil($value/$gbpExchangeRate);*/
			
			$this->CI->load->model('country_model');
			$this->CI->load->model('product_model');
			
			if ($prod_id){
				$productCurrent = $this->CI->product_model->get_by_id($prod_id);
				
				if ($productCurrent->parent_id){
					$prod_id = $productCurrent->parent_id;
				}
			}
			
			if ($prod_id){
				$providerBonus = $this->CI->product_model->getProviderBonus($prod_id);
				$providerBonus = 1 + $providerBonus/100;
			} else {
				$providerBonus = 1;
			}
			
			if ($providerBonus == 0){
				$providerBonus = 1;
			}

			$greatBritain = $this->CI->country_model->get_by_id($this->CI->config->item('def_country'));
			$britVat = 1 + ($greatBritain->vat/100);
			
			if (!$countryId){
				$countryId = ($this->CI->session->userdata('selected_country') ? $this->CI->session->userdata('selected_country'):$this->CI->config->item('def_country'));
			}

			$currentCountry = $this->CI->country_model->get_by_id($countryId);

			$currentVat = (!$no_vat) ? $currentCountry->vat: 0;
			
			$priceGap = $currentCountry->price_gap;
			$kedvezmeny = 0;
			if ($discount_rate){
				$kedvezmeny = $discount_rate;
			}
	
			$value = round((($value/$britVat)/$providerBonus - $value*$kedvezmeny/100)*(1 + $currentVat/100)*(1 +$priceGap/100),2);
			
			if ($currency == 'EUR'){
				$currencyObj = $this->CI->currency_model->get_currency('GBP');
				$exchangeRate = $this->CI->exchange_rate_model->get_rate_by_currency($currencyObj->id);
			
				return ceil($value/$exchangeRate->value*(1-($promotion/100)));
			}
		}
		
		$currencyObj = $this->CI->currency_model->get_currency($currency);
		$exchangeRate = $this->CI->exchange_rate_model->get_rate_by_currency($currencyObj->id);

		return ceil($value*$exchangeRate->value*(1-($promotion/100)));
	}
	
	/**
	 *
	 * @param integer $value
	 * @param string $currency
	 */
	public function getTax($value, $currency = '',  $discount_rate = false){
		if (empty($currency)){
			$currency = $this->CI->config->item('def_currency');
		}

		$this->CI->load->model('country_model');
		
		$greatBritain = $this->CI->country_model->get_by_id($this->CI->config->item('def_country'));
		$britVat =  $greatBritain->vat/100;
		if ($discount_rate){
			$value = $value*$britVat /(1 + ($discount_rate/ 100) ) ;
		} else {
			$value = $value*$britVat;
		}
		
		
		if ($currency = 'EUR'){
			$currencyObj = $this->CI->currency_model->get_currency('GBP');
			$exchangeRate = $this->CI->exchange_rate_model->get_rate_by_currency($currencyObj->id);

			return ceil($value/$exchangeRate->value);
		}
	
		$currencyObj = $this->CI->currency_model->get_currency($currency);
		$exchangeRate = $this->CI->exchange_rate_model->get_rate_by_currency($currencyObj->id);
		
		return ceil($value*$exchangeRate->value);
	}
	
	public function transformProductPrice($id, $currency = ''){
		if (empty($currency)){
			$currency = $this->CI->config->item('def_currency');
		}
	}
}